package com.example.demo_widget.rerecyclerview

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.demo_widget.R

/**
 * 一。recyclerview的四级缓存
 * 1.屏幕内缓存
 * 2.屏幕外缓存：mCacheViews，默认2个（复用不需要onCreate和onBind）
 * 3.recyclePool:一种类型默认5个（复用不需要onCreate，需要onBind）
 * 4.自定义缓存
 *
 * 二。recyclerview和listview的区别
 * 1.recyclerview支持复用，listview需要手动复用
 * 2.recyclerview支持局部刷新，listview不支持
 *
 *
 * 三。recyclerview优化
 * 1.避免全局刷新
 *  (1)支持局部刷新:notifyItemXxx
 *  (2)局部增量刷新payloads（点赞、收藏）:notifyItemChanged(position,payloads) 重写onBindViewHolder(xx,xx,payload)
 *
 *
 * 四。recyclerview加载大量数据，如何优化
 */
class RecyclerviewActivity : AppCompatActivity() {
    private lateinit var rv:RecyclerView
    private lateinit var adapter: MyAdapter
    private val list = listOf(
        PersonEntity("11","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("22","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("33","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("44","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("55","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("666","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("77","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("88","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("99","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        PersonEntity("101010","https://img2.baidu.com/it/u=2884331298,2763879521&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
        )
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_recyclerview)
        rv = findViewById(R.id.rv)
        adapter = MyAdapter()
        adapter.setData(list)
        rv.adapter = adapter
        rv.layoutManager = LinearLayoutManager(this)


    }
    //测试局部增量刷新
    fun like(view: View) {
        adapter.like(0)
    }
}